<?php
namespace app\admin\controller;

use \think\Db;
use \think\Cookie;
use \think\Session;
use app\admin\model\AdminMenu;
use app\admin\controller\User;
class Problems extends User
{
    /**
     * 答题详情
     * @return mixed
     */
    public function index()
    {
        $where = array();
        if($_POST){
            if(!empty($_POST['username'])){
                $where['username'] = $_POST['username'];
            }
            if(!empty($_POST['mobile'])){
                $where['mobile'] = $_POST['mobile'];
            }
            if(!empty($_POST['company'])){
                $where['company'] = $_POST['company'];
            }
        }

        if(!empty($where)){
            $data = Db::name('answer')->order('id desc')->where($where)->paginate(10);
            $this->assign("where",$where);
        }else{
            $data = Db::name('answer')->order('id desc')->paginate(10);
        }

        $page = $data->render();
        $this->assign('page', $page);

        $data = o2a($data);
        $list = $data['data'];

        foreach($list as $key => $value){
            $city_info = Db::name('city')->where('id',$value['region'])->find();
            $list[$key]['city'] = $city_info['city'];
        }

        //分页显示输出
        $this->assign("list",$list);
        return $this->fetch();
    }

    /**
     * 题目列表
     * @return mixed
     */
    public function problemsList(){
        $data = Db::name('problems')->order('id desc')->paginate(10);
        //分页显示输出
        $page = $data->render();
        $this->assign('page', $page);
        $data = o2a($data);
        $list = $data['data'];
        foreach ($list as $key => $value){
            $list[$key]['select'] = json_decode($value['select'],true);
        }
        $this->assign("list",$list);
        return view();
    }

    /**
     * 添加题目
     * @return mixed
     */
    public function addProblem(){
        if ($this->request->isPost()){
            //获取数据
            $data = $this->request->post();
            $select = $data['select'];
            foreach($select as $key => $value){
                if(empty($value)){
                    unset($select[$key]);
                }
            }
            $insert = ['type'=>$data['type'],'topic'=>$data['topic'],'select'=>json_encode($select),'answer'=>$data['answer']];
            $res = Db::name('problems')->insert($insert);
            if($res>0) {
                return $this->success('增加成功','admin/problems/problemsList');
            } else {
                return $this->error('增加失败');
            }
        }
        return $this->fetch();
    }

    /**
     * 修改题目
     * @return mixed|void
     */
    public function editProblem(){

        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if($this->request->isPost()){
            //获取数据
            $data = $this->request->post();
            $select = $data['select'];
            foreach($select as $key => $value){
                if(empty($value)){
                    unset($select[$key]);
                }
            }
            $update = ['type'=>$data['type'],'topic'=>$data['topic'],'select'=>json_encode($select),'answer'=>$data['answer']];
            $res = Db::name('problems')->where('id',$data['id'])->update($update);
            if($res !==false){
                return $this->success('修改成功','admin/problems/problemsList');
            }else{
                return $this->error('修改失败');
            }
        }else{
            if($id>0){
                $info = Db::name('problems')->where('id',$id)->find();
                $info['select'] = json_decode($info['select'],true);
                $this->assign('info',$info);
                return $this->fetch();
            }else{
                return $this->error('操作错误');
            }
        }
    }

    public function delProblem(){
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if($id > 0) {

                if(false == Db::name('problems')->where('id',$id)->delete()) {
                    return $this->error('删除失败');
                } else {
                    addlog($id);//写入日志
                    return $this->success('删除成功','admin/problems/problemsList');
                }
            } else {
                return $this->error('id不正确');
            }
        }
    }

}
